package com.bsc.rtc.lib.libraries.MyViewDemo.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.bsc.rtc.lib.libraries.utils.CalenderUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MCalenderView extends View {
    List<Region> regionList;
    String TAG = "MCalenderView";
    Paint paint;
    Calendar calendar;
    int titleHeight = 100;
    int commPadding = 50;
    int dateHeight;
    int itemWidth;
    CalenderUtils calenderUtils;

    public MCalenderView(Context context) {
        this(context, null);
    }

    public MCalenderView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MCalenderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(10f);
        calendar = Calendar.getInstance();
        calenderUtils = new CalenderUtils();
        regionList=new ArrayList<>();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawDate(canvas);
        drawWeekDay(canvas);
        drawEveryDay(canvas);

    }

    /**
     * @auth zzw
     * @Date 2019/10/25 10:35
     * @Desc 获取年月日
     */
    private void getDate() {


    }

    /**
     * @auth zzw
     * @Date 2019/10/25 10:34
     * @Desc 绘制年月日
     */
    private void drawDate(Canvas canvas) {
        paint.setTextSize(50);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String date = year + " " + " " + month + " " + day;
        Rect rect = new Rect();
        paint.getTextBounds(date, 0, date.length(), rect);
        dateHeight = rect.bottom - rect.top;
        int start = (getMeasuredWidth() - (rect.right - rect.left)) / 2;
        canvas.drawText(date, start, titleHeight, paint);
    }

    /**
     * @auth zzw
     * @Date 2019/10/25 10:03
     * @Desc 绘制星期
     */
    private void drawWeekDay(Canvas canvas) {
        itemWidth = getMeasuredWidth() / 7;
        paint.setTextSize(50);
        Rect rect = new Rect();
        paint.getTextBounds("Sun", 0, 2, rect);
        int textWidth = rect.right - rect.left;
        int padding = (itemWidth - textWidth) / 2;
        canvas.drawText("Mon", padding, titleHeight + dateHeight + commPadding, paint);
        canvas.drawText("Tue", itemWidth + padding, titleHeight + dateHeight + commPadding, paint);
        canvas.drawText("Wen", itemWidth * 2 + padding, titleHeight + dateHeight + commPadding, paint);
        canvas.drawText("Thi", itemWidth * 3 + padding, titleHeight + dateHeight + commPadding, paint);
        canvas.drawText("Fri", itemWidth * 4 + padding, titleHeight + dateHeight + commPadding, paint);
        canvas.drawText("Sat", itemWidth * 5 + padding, titleHeight + dateHeight + commPadding, paint);
        canvas.drawText("Sun", itemWidth * 6 + padding, titleHeight + dateHeight + commPadding, paint);
    }

    /**
     * @auth zzw
     * @Date 2019/10/25 10:53
     * @Desc 绘制每一天
     */
    private void drawEveryDay(Canvas canvas) {
        int dateNumbers = calenderUtils.getDateNumForDate(new Date());
        Log.i(TAG, "dateNumbers" + dateNumbers);
        calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        Log.i(TAG, "DAY_OF_WEEK " + day);
        Rect rect = new Rect();
        paint.getTextBounds("Sun", 0, 2, rect);
        int textWidth = rect.right - rect.left;
        int padding = (itemWidth - textWidth) / 2;

        for (int i = day-1; i < dateNumbers+day-1; i++) {
            int itemHeight = titleHeight * 2 + (dateHeight * ((i / 7 + 1))) + commPadding;
            Log.i(TAG, "itemHeight " + itemHeight+" "+i);
            canvas.drawText(i + "", (itemWidth * (i % 7)) + padding, itemHeight, paint);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float downX = event.getX();
                float downY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
    /**
     *@auth zzw
     *@Date 2019/10/25 14:04
     *@Desc 点击区域判断
     */
    private void clickDay(int x,int y){

    }
}
